package com.example.demo.jpython;

import org.python.util.PythonInterpreter;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @Create: IntelliJ IDEA.
 * @Author: subtlman_ljx
 * @Date: 2022/10/18/10:24
 * @Description:
 */
public class demo1 {
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("a=[5,2,3,9,4,0]; ");
        interpreter.exec("print(sorted(a));"); //此处python语句是3.x版本的语法
        interpreter.exec("print sorted(a);"); //此处是python语句是2.x版本的语法




        Process proc;
        try {
            // 执行py文件
            proc = Runtime.getRuntime().exec("D:\\python\\python.exe D:\\python\\workspace\\space1\\demo\\test\\A.py");
            // 用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in .readLine()) != null) {
                System.out.println(line);
            }
            in .close();
            proc.waitFor();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}
